package com.feiyu.paymentAndcredit.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.feiyu.paymentAndcredit.dao.*;
import com.feiyu.paymentAndcredit.dto.PaymentDto;
import com.feiyu.paymentAndcredit.entity.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feiyu.common.utils.PageUtils;
import com.feiyu.common.utils.Query;

import com.feiyu.paymentAndcredit.service.DealService;

import javax.annotation.Resource;


@Service("dealService")
public class DealServiceImpl extends ServiceImpl<DealDao, DealEntity> implements DealService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<DealEntity> wrapper = new QueryWrapper<>();
        if(!(params.get("mainTraderId")=="")){
            wrapper.eq("main_trader_id",params.get("mainTraderId"));
        }
        IPage<DealEntity> page = this.page(
                new Query<DealEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }


    @Resource
    DealDao dealDao;

    @Resource
    BuyerDao buyerDao;

    @Resource
    ShopDao shopDao;

    @Resource
    LeaseOrderDao orderDao;

    @Resource
    LogisticsInfoDao logisticsInfoDao;

    @Override
    public boolean changeAccountMoney(PaymentDto dto) {

        insertDealInfo(dto);

        if (dto.getPowerLv() == 10) {

            QueryWrapper<BuyerEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",dto.getUserId());

            BuyerEntity info = buyerDao.selectOne(wrapper);
            float money = info.getBuyerMoney();
            info.setBuyerMoney(money+dto.getDealAmount());

            UpdateWrapper<BuyerEntity> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("user_id",dto.getUserId());

            buyerDao.update(info,updateWrapper);

        } else {
            QueryWrapper<ShopEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",dto.getUserId());

            ShopEntity info = shopDao.selectOne(wrapper);
            float money = info.getMoney();
            info.setMoney(money+dto.getDealAmount());

            UpdateWrapper<ShopEntity> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("user_id",dto.getUserId());

            shopDao.update(info,updateWrapper);


        }

        return true;
    }


    @Override
    public boolean insertDealInfo(PaymentDto dto) {

        DealEntity deal = new DealEntity();
        deal.setMainTraderId(dto.getUserId());
        deal.setDealAmount(dto.getDealAmount());
        deal.setDealTime(new Date());
        deal.setMinorTrader(dto.getMinorTrader());
        deal.setDealIntro(dto.getDealIntro());

        dealDao.insert(deal);

        return true;
    }

    @Override
    public boolean updateBuyerCreditScore(String orderId) {

        QueryWrapper<LeaseOrderEntity> orderWrapper = new QueryWrapper<>();
        orderWrapper.eq("order_id",orderId);

        int creditC = 0;
        LeaseOrderEntity orderInfo = orderDao.selectOne(orderWrapper);
        //损坏-15，租赁完成+10，转租为买+5
        switch (orderInfo.getOrderState()) {
            case 9:creditC = -15;break;
            case 5:creditC = 5;break;
            case 7:creditC = 10;break;
            default:break;
        }

        //根据租赁时间是否超时减分
        QueryWrapper<LogisticsInfoEntity> logisticsWrapper = new QueryWrapper<>();
        logisticsWrapper.eq("logistics_id",orderInfo.getLogisticsSendId());
        LogisticsInfoEntity logisticsInfo = logisticsInfoDao.selectOne(logisticsWrapper);

        long days = logisticsInfo.getLogisticsCreateTime().compareTo(orderInfo.getOrderCreatTime())/(60*60*24) ;
        if (days > 0) {
            //每超时一天  -1
            creditC-=days;
        }



        QueryWrapper<BuyerEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",orderInfo.getBuyerId());

        BuyerEntity buyerInfo = buyerDao.selectOne(wrapper);
        buyerInfo.setBuyerCredit(buyerInfo.getBuyerCredit()+creditC);

        UpdateWrapper<BuyerEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id",orderInfo.getBuyerId());

        buyerDao.update(buyerInfo,updateWrapper);

        return true;
    }
}